#!/usr/bin/python

import os
import cgi
from nspot import SystemMessages, Arp, SystemProperties, UserDAO, BlockedDAO, MessageDAO, FirewallUtil, UserHandler
import cgitb; cgitb.enable()
import datetime
from Cheetah.Template import Template



form = cgi.FieldStorage()

sp = SystemProperties()
arp = Arp()
ud = UserDAO()
sm = SystemMessages()
ip = os.environ['REMOTE_ADDR']

t = Template(file = sp.get_web_root()+'/htdocs/pages/templates/baselayout.html')


username = form.getvalue('username')
password = form.getvalue('password')
newpassword = form.getvalue('newpassword')
toopassword = form.getvalue('toopassword')

t.msg_login = sm.msg_login()
t.page_title = sm.get_title()
t.head_redirect = "no" # can be yes or no
t.head_to_login = "no" # can be yes or no
t.body_page = "none" # can be login, register or none
t.body_message = ''

mac = arp.get_mac_from_arp(ip)
user = ud.retrieve_by_mac(mac)

        
print "Content-type: text/html\n\n"
        
if username == None or password == None or newpassword == None or toopassword == None or len(username) < 1 or len(password) < 1 or len(newpassword) < 1 or len(toopassword) < 1:
    t.system_message = sm.msg_allfields()
    print t
        
elif user == None:
    t.system_message = sm.msg_notregistered()
    print t
            
elif ip != user.ip:
    t.system_message = sm.msg_wrongip()+" "+sm.msg_support()
    print t
            
elif username != user.email or password != user.password:
    t.system_message = sm.msg_wrongpassword()
    print t
            
else :
    ud.update_password(user.id,newpassword)
    t.body_page = 'none' # can be login, register or none
    t.head_to_login = "yes" # can be yes or no
    t.to_login = sm.get_head_to_login()
    t.system_message = sm.msg_change_ok()
	    
    print t
